﻿<div>
    <RadioGroup @bind-Value="selectionType">
        <Radio Value="SelectionType.Checkbox">Checkbox</Radio>
        <Radio Value="SelectionType.Radio">Radio</Radio>
    </RadioGroup>

    <h5>
        selections: 
        @if (selectedRows != null)
        {
            @foreach (var selected in selectedRows)
            {
                <Tag @key="selected" Closable OnClose="e => RemoveSelection(selected.Name)">@selected.Name</Tag>
            }
            <Button Danger Size="ButtonSize.Small" OnClick="@(e => { table.SetSelection([]); })">Clear</Button>
        }
    </h5>

    <Table @ref="table" DataSource="@data" @bind-SelectedRows="selectedRows" RowKey="x => x.Name" OnSelectAll="OnSelectAll">
        <Selection Type="@selectionType" Disabled="@(context.Name == "Disabled User")" />
        <PropertyColumn Property="c=>c.Name">
            <a>@context.Name</a>
        </PropertyColumn>
        <PropertyColumn Property="c=>c.Age" />
        <PropertyColumn Property="c=>c.Address" />
    </Table>
</div>

@code {

    ITable table;

    class Column
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public string Address { get; set; }
    }

    Column[] data =
    {
        new Column()
        {
            Name = "John Brown",
            Age = 32,
            Address = "New York No. 1 Lake Park",
        },
        new Column()
        {
            Name = "Jim Green",
            Age = 42,
            Address = "London No. 1 Lake Park",
        },
        new Column()
        {
            Name = "Joe Black",
            Age = 32,
            Address = "Sidney No. 1 Lake Park",
        },
        new Column()
        {
            Name = "Disabled User",
            Age = 99,
            Address = "Sidney No. 1 Lake Park",
        }
    };

    IEnumerable<Column> selectedRows;
    SelectionType selectionType = SelectionType.Checkbox;

    protected override void OnInitialized()
    {
    }

    public void RemoveSelection(string key)
    {
        this.selectedRows = selectedRows.Where(x => x.Name != key).ToList();
    }

    void OnSelectAll(bool selectAll)
    {
        Console.WriteLine($"select all row? {selectAll}");
    }
}